package com.example.service.tribune;

import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.exception.CustomException;
import com.example.mapper.tribune.BlogMapper;
import com.example.mapper.tribune.LikeMapper;
import com.example.mapper.tribune.PostMapper;
import com.example.model.entity.other.Account;
import com.example.model.entity.tribune.Blog;
import com.example.model.entity.tribune.Collect;
import com.example.model.entity.tribune.Like;
import com.example.model.vo.PostVO;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 点赞信息表业务处理
 **/
@Service
public class LikeService {

    @Resource
    private LikeMapper likeMapper;
    @Resource
    private PostMapper postMapper;
    @Resource
    private BlogMapper blogMapper;

    /**
     * 新增
     */
    public void add(Like like) {
        likeMapper.insert(like);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        likeMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            likeMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Like like) {
        likeMapper.updateById(like);
    }

    /**
     * 根据ID查询
     */
    public Like selectById(Integer id) {
        return likeMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Like> selectAll(Like like) {
        return likeMapper.selectAll(like);
    }

    /**
     * 分页查询
     */
    public PageInfo<Like> selectPage(Like like, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Like> list = likeMapper.selectAll(like);
        return PageInfo.of(list);
    }

    public void save(Like like) {
        Account currentUser = TokenUtils.getCurrentUser();
        Like dbLike=new Like();
        if(RoleEnum.USER.name().equals(currentUser.getRole())){
            like.setUserId(currentUser.getId());
            dbLike = likeMapper.selectByUserIdAndFid(like.getUserId(), like.getFid(),like.getModule());
        }else if(RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
            like.setDoctorId(currentUser.getId());
            dbLike = likeMapper.selectByDoctorIdAndFid(like.getDoctorId(), like.getFid(),like.getModule());
        }
        if (dbLike != null) {  // 说明收藏过了
            likeMapper.deleteById(dbLike.getId());  //删除收藏
            if(like.getModule().equals("POST")){
                PostVO postVO = postMapper.selectById(like.getFid());
                Integer clickNum = postVO.getClickNum();
                postVO.setClickNum(clickNum -1);
                postMapper.updateById(postVO);
            }else if(like.getModule().equals("BLOG")){
                Blog blog = blogMapper.selectById(like.getFid());
                Integer clickNum =  blog.getClickNum();
                blog.setClickNum(clickNum -1);
                blogMapper.updateById( blog);
            }
            throw new CustomException(ResultCodeEnum.CLICK_ALREADY_NO);
        } else {
            // 新的收藏
            likeMapper.insert(like);
            if(like.getModule().equals("POST")){
                PostVO postVO = postMapper.selectById(like.getFid());
                Integer clickNum = postVO.getClickNum();
                postVO.setClickNum(clickNum+1);
                postMapper.updateById(postVO);
            }else if(like.getModule().equals("BLOG")){
                Blog blog = blogMapper.selectById(like.getFid());
                Integer clickNum =  blog.getClickNum();
                blog.setClickNum(clickNum +1);
                blogMapper.updateById( blog);
            }
        }
    }

    public Like selectByUserId(Integer userId, Integer fid, String module) {
        Account currentUser = TokenUtils.getCurrentUser();
        Like dbLike=new Like();
        if(RoleEnum.USER.name().equals(currentUser.getRole())){
            dbLike = likeMapper.selectByUserIdAndFid(userId,fid,module);
        }else if(RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
            dbLike = likeMapper.selectByDoctorIdAndFid(userId, fid,module);
        }
        return dbLike;
    }
}
